<?php
declare(strict_types=1);

namespace ServerApp\lib\server\web_socket;

use ServerApp\lib\core\SWebSocketServer;
use ServerApp\lib\server\udp\Udp;
use stdClass;
use Swoole\Server;
use WebApp\lib\config\Config;
use WebApp\lib\db\RedisPool;
use Swoole\Coroutine\Redis;

/**
 * Class Http
 */
class WebSocket extends SWebSocketServer
{
    private $func;

    /**
     * Conf constructor
     * @param IWebSocket $func1
     */
    public function __construct(IWebSocket $func1)
    {
        $this->func = $func1;
        parent::__construct();
    }

    /**
     * @return string
     */
    protected function getMaster(): string
    {
        return $this->func->getName() . "_m1";
    }

    /**
     * @return string
     */
    protected function getManager(): string
    {
        return $this->func->getName() . "_m2";
    }

    /**
     * @return string
     */
    protected function getWorker(): string
    {
        return $this->func->getName() . "_w";
    }

    /**
     * @return int
     */
    public function getPort(): int
    {
        return PORT;
    }

    /**
     * @return string
     */
    protected function getBind(): string
    {
        return "0.0.0.0";
    }

    /**
     * @return array
     */
    protected function getSetting(): array
    {
        return array_merge(
            Config::getConf('server.setting'),
            Config::getConf('server.children.' . SERVER_NAME)
        );
    }

    protected function Start(): void
    {
        tracker(I, 'master start');
    }

    protected function ManagerStart(): void
    {
        tracker(I, 'manager start');
    }

    private $key;

    /**
     * @param $server
     * @param $worker_id
     */
    protected function WorkerStart(Server $server, $worker_id): void
    {
        tracker(I, "WorkerStart $worker_id");

//        $tmp = new stdClass();
//        $host = crc32($this->getHost());
//        $this->key = "list_{$host}";
//        $tmp->func = function() use($tmp){
//            while ($data = RedisPool::getInstance()->exec(function(Redis $client){
//                return $client->rPop($this->key);
//            })) {
//                list($str, $time) = explode("\v", $data);
//                if (time() > $time) {
//                    continue;
//                }
//                list($to, $mid, $data) = explode(Udp::SPLIT, $str);
//                $this->func->udpReceiver($to, $mid, $data);
//            }
//            swoole_timer_after(1000, $tmp->func);
//        };
//        ($tmp->func)();
    }

    /**
     * @param $server
     * @param $fd
     */
    protected function Close(Server $server, $fd): void
    {
//        tracker(I, 'close');
    }

    /**
     * @param $server
     * @param $worker_id
     */
    protected function WorkerStop(Server $server, $worker_id): void
    {
        tracker(I, "WorkerStop $worker_id");
    }

    /**
     * @return bool
     */
    protected function isSsl(): bool
    {
        return false;
    }

    /**
     * @param $server
     * @param $fd
     * @param $path
     */
    protected function Open($server, $fd, $path): void
    {
        $this->func->open($server, $fd, $path);
    }

    /**
     * @param $server
     * @param $fd
     * @param $data
     */
    protected function Message($server, $fd, $data): void
    {
        $this->func->message($server, $fd, $data);
    }
}